package com.gitee.chili.leisure.message;

import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.RoomManager;
import com.gitee.chili.leisure.SimpleBeanFactory;
import com.gitee.chili.leisure.core.AbstractDefaultMessage;
import com.gitee.chili.leisure.core.Packet;
import com.gitee.chili.leisure.domain.dto.LoginQuery;
import com.gitee.chili.leisure.domain.dto.MemberVo;
import com.gitee.chili.leisure.model.BaseMessageModel;
import com.gitee.chili.leisure.model.MessageCmdEnum;
import com.gitee.chili.leisure.model.PlayerModel;
import com.gitee.chili.leisure.service.SpecificTaskTransferClient;
import com.gitee.chili.leisure.util.AuxiliaryRoom;
import com.gitee.chili.leisure.util.Connection;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * Login
 *
 * @author week solider
 * @since 2020.08.31
 */
@Slf4j
public class Login extends AbstractDefaultMessage {

    private MemberVo member;


    public Login(Packet packet, Channel channel) {
        super(packet, channel);
    }

    @Override
    protected void decoder(byte[] body) throws InvalidProtocolBufferException {

        SpecificTaskTransferClient client = SimpleBeanFactory.getBean(SpecificTaskTransferClient.class);

        BaseMessageModel.LoginCmd loginCmd = BaseMessageModel.LoginCmd.parseFrom(body);

        // 登录
        ResponseResult<MemberVo> result = client.login(warpData(loginCmd));

        asserts(result);

        member = result.getData();

    }

    @Override
    public Packet parseBody() {

        // 通过信道传输 userId , 此时已经不需要客户端额外传递玩家id
        getChannel().attr(AttributeKey.valueOf("userId")).set(member.getUserId());

        PlayerModel.NotifyPlayerInfo.Builder builder = PlayerModel.NotifyPlayerInfo.newBuilder();
        builder.setProvince(member.getAreaProvince().getName())
                .setCity(member.getAreaCity().getName())
                .setExp(member.getExp().longValue())
                .setNickname(member.getNickname())
                .setGender(member.getGender())
                .setCoins(member.getScore())
                .setLevel(member.getLevel())
                .setGems(member.getGems());

        return Packet.newPacket(MessageCmdEnum.Command.NOTIFY_PLAYER_INFO, builder.build().toByteArray());
    }

    /**
     * 包装登录请求对象
     *
     * @param loginCmd 登录命令数据
     */
    private LoginQuery warpData(BaseMessageModel.LoginCmd loginCmd) {

        return LoginQuery.builder()
                .code(loginCmd.getVerifyCode())
                .loginWay(loginCmd.getLoginWayValue())
                .username(loginCmd.getUsername())
                .password(loginCmd.getPassword())
                .token(loginCmd.getToken())
                .phone(loginCmd.getPhone())
                .build();
    }


    /**
     * 重新连接
     */
    public void reconnection() {
        Long roomNum = member.getRoomNum();
        if (RoomManager.hasRoomNum(roomNum)) {
            Connection.broadcaster().notify(roomNum, AuxiliaryRoom.reconnection(roomNum));
        }
    }

}
